package com.etool.action.yuanlai;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 自动生成统计脚本
 * @author linjia
 *
 */
public class StatisticsScriptGenerateAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	HttpServletRequest request=ServletActionContext.getRequest();
	HttpServletResponse response=ServletActionContext.getResponse();
	
	private String table;
	
	private String[] fields;
	
	private String[] keys;
	
	private String[] types;
	
	private String[] sqls;
	
	private List<Map<String, Object>> columns;
	
	@Action("statistics-script-generate-index")
	public String statisticsScriptGenerateIndex()
	{
		
		return "SUCCESS";
	}
	
	
	@Action("statistics-script-generate")
	public String statisticsScriptGenerate()
	{
		
		columns=new ArrayList<Map<String,Object>>();
		
		String keyStr="";
		String keyStr2="";
		
		for(int i=0;i<fields.length;i++)
		{
			if(StringUtils.isEmpty(fields[i]))
			{
				continue;
			}
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("field",fields[i]);
			map.put("key",keys[i]);
			map.put("type", types[i]);
			map.put("sql", sqls[i]);
			columns.add(map);
			
			
			if(StringUtils.equals("true", keys[i]))
			{
				keyStr+=fields[i]+",";
				keyStr2+="\""+fields[i]+"\" ";
			}
			
		}
		request.setAttribute("keyStr", keyStr.substring(0, keyStr.length()-1));
		request.setAttribute("keyStr2", keyStr2.substring(0, keyStr2.length()-1));
		return SUCCESS;
	}

	public String[] getFields() {
		return fields;
	}

	public void setFields(String[] fields) {
		this.fields = fields;
	}

	public String[] getKeys() {
		return keys;
	}

	public void setKeys(String[] keys) {
		this.keys = keys;
	}

	public String[] getTypes() {
		return types;
	}

	public void setTypes(String[] types) {
		this.types = types;
	}

	public String[] getSqls() {
		return sqls;
	}

	public void setSqls(String[] sqls) {
		this.sqls = sqls;
	}


	public List<Map<String, Object>> getColumns() {
		return columns;
	}


	public void setColumns(List<Map<String, Object>> columns) {
		this.columns = columns;
	}


	public String getTable() {
		return table;
	}


	public void setTable(String table) {
		this.table = table;
	}
	
	
}
